<template>
  <div class="box">
    <div class="logincontentbox">
      <h2>电商后台管理系统</h2>
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input v-model="ruleForm.username" prefix-icon="el-icon-user">
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="loginHandle('ruleForm')"
            >登录</el-button
          >
          <el-button @click="cancelHandle('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { loginapi } from "@/http/api";
import { Message } from "element-ui";
export default {
  name: "",
  components: {},
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 6, message: "长度在 3 到 6 个字符", trigger: "blur" },
        ],
      },
    };
  },
  created() {},
  mounted() {},
  methods: {
    loginHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (!valid) return false;
        loginapi(this.ruleForm).then((res) => {
          console.log(res);
          if (res.data.meta.status == 200) {
            Message({
              message: res.data.meta.msg,
              type: "success",
            });
            this.$router.push("/home");
            sessionStorage.setItem("token", res.data.data.token);
          }
        });
      });
    },
    cancelHandle(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
<style scoped lang="scss">
.box {
  width: 100vw;
  height: 100vh;
  background-color: #284a69;
  position: relative;
  .logincontentbox {
    width: 40%;
    height: 300px;
    background-color: #fff;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    border-radius: 10px;
    box-shadow: 0 0 10px #fff;
    text-align: center;
    min-width: 400px;
    box-sizing: border-box;
    padding: 30px;
  }
}
</style>
